package cn.exrick.manager.service.hotclassload;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class TestCGLIB {

    public void execule(){
        System.out.println("执行目标方法。。");
    }

    public void del(){
        System.out.println("执行目标方法del...");
    }


    static class CGLIBProxy implements MethodInterceptor {


        public CGLIBProxy(){
        }

        public <T> T createProxy(Class<T> cl){
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(cl);
            enhancer.setCallback(this);
            return (T)enhancer.create();
        }

        @Override
        public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
            System.out.println("执行方法Before："+method.getName());
            Object result = methodProxy.invokeSuper(o,objects);
            System.out.println("执行方法After："+method.getName());
            return result;
        }
    }

    public static void main(String[] args) throws Exception{
        CGLIBProxy cglibProxy = new CGLIBProxy();
        TestCGLIB t = cglibProxy.createProxy(TestCGLIB.class);
        t.execule();
        t.del();
    }
}
